//
//  GCProjectile.m
//  GalaxyConqueror
//
//  Created by  Stefan Nestorov on 8/16/14.
//  Copyright (c) 2014 Stefan.Nestorov. All rights reserved.
//

#import "GCProjectile.h"

static const int PROJECTILE_SPEED = 300;

@implementation GCProjectile

-(instancetype)initWithShootingObject:(SKSpriteNode *)shootingObject
{
    
    SKTextureAtlas *spritesAtlas = [GCGlobals textureAtlasNamed:@"sprites"];
    
    NSString *textureName = (shootingObject.physicsBody.categoryBitMask == PLAYER_SHIP_CATEGORY) ? @"PlayerMissile_1" : @"EnemyMissile_1";
    
    self = [super initWithTexture:[spritesAtlas textureNamed:textureName]];
    if ( self ) {
        
        [self setupPhysicsWithShootingObject:shootingObject];
        [self setPositionFromShootingObject:shootingObject];
        
        _damage = 10;
        
    }
    
    return self;
}


-(void)setupPhysicsWithShootingObject:(SKSpriteNode *)shootingObject
{
    
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
    [self skt_attachDebugRectWithSize:self.size color:[SKColor redColor]];
    self.physicsBody.affectedByGravity = NO;
    self.physicsBody.dynamic = NO;
    
    if ( shootingObject.physicsBody.categoryBitMask == PLAYER_SHIP_CATEGORY ) {
        self.physicsBody.categoryBitMask = PLAYER_PROJECTILE_CATEGORY;
        self.physicsBody.contactTestBitMask = ENEMY_SHIP_CATEGORY;
    } else {
        self.physicsBody.categoryBitMask = ENEMY_PROJECTILE_CATEGORY;
        self.physicsBody.contactTestBitMask = PLAYER_SHIP_CATEGORY;
    }
    self.physicsBody.collisionBitMask = 0;
    
}


-(void)setPositionFromShootingObject:(SKSpriteNode *)shootingObject
{
    
    if ( shootingObject.physicsBody.categoryBitMask == PLAYER_SHIP_CATEGORY ) {
        [self setPosition:CGPointMake(shootingObject.position.x + (shootingObject.size.width / 2), shootingObject.position.y)];
    } else {
        [self setPosition:CGPointMake(shootingObject.position.x - (shootingObject.size.width / 2), shootingObject.position.y)];
    }
    
}


-(void)update:(CFTimeInterval)dt
{
    
    if ( self.physicsBody.categoryBitMask == PLAYER_PROJECTILE_CATEGORY ) {
        
        if ( (self.position.x + (PROJECTILE_SPEED * dt) < self.scene.size.width + self.size.width / 2) &&
             (self.position.x + (PROJECTILE_SPEED * dt) > self.size.width / 2) ) {
            
            self.position = CGPointMake(self.position.x + (PROJECTILE_SPEED * dt), self.position.y);
            
        } else {
            _shouldBeRemoved = YES;
        }
        
        
    } else {
        if ( (self.position.x - (PROJECTILE_SPEED * dt) < self.scene.size.width + self.size.width / 2) &&
            (self.position.x - (PROJECTILE_SPEED * dt) > -self.size.width / 2) ) {
            
            self.position = CGPointMake(self.position.x - (PROJECTILE_SPEED * dt), self.position.y);
            
        } else {
            _shouldBeRemoved = YES;
        }
        
//        self.position = CGPointMake(self.position.x - (PROJECTILE_SPEED * dt), self.position.y);
    }
    
}

-(void)dealloc
{
    
    NSLog(@"Dealloc %@",[self class]);
    
}


@end
